package com.domdd.dao.common;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.domdd.model.InventoryNew;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.core.parameters.P;

import java.util.Date;
import java.util.List;

public interface InventoryNewMapper extends BaseMapper<InventoryNew> {

    void replace(@Param("records") List<InventoryNew> inventoryNews);

    default IPage<InventoryNew> selectByPage(IPage<InventoryNew> page, Date startTime, Date endTime, List<String> ignoreOuterIdList) {
        LambdaQueryWrapper<InventoryNew> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(InventoryNew::getInventoryDate, startTime)
                .lt(InventoryNew::getInventoryDate, endTime);
        if (CollectionUtil.isNotEmpty(ignoreOuterIdList)) {
            wrapper.notIn(InventoryNew::getOuterId, ignoreOuterIdList);
        }
        return this.selectPage(page, wrapper);
    }

    default IPage<InventoryNew> selectByPageAndInventoryDate(IPage<InventoryNew> page, Date inventoryDate) {
        LambdaQueryWrapper<InventoryNew> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InventoryNew::getInventoryDate, inventoryDate);
        wrapper.orderByAsc(InventoryNew::getId);
        return this.selectPage(page, wrapper);
    }

    default void deleteByInventoryDate(DateTime inventoryDate) {
        this.delete(
                new LambdaQueryWrapper<InventoryNew>()
                        .in(InventoryNew::getInventoryDate, inventoryDate)
        );
    }

    default List<InventoryNew> selectByInventoryDate(Date inventoryDate) {
        return this.selectList(new LambdaQueryWrapper<InventoryNew>()
                .eq(InventoryNew::getInventoryDate, inventoryDate)
                .orderByAsc(InventoryNew::getId));
    }
}